package prgechecsia;

import prgechecsmodel.IIHM;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import prgechecsimpla.Board;
import prgechecsmodel.IIA;

public class IHM implements IIHM {

    private IIA pIA;
    public static BufferedReader reader;

    private IHM() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        lineInput();
    }

    @Override
    public void lineInput() throws IOException {
        Board board = new Board();
        board.setupStart();
        System.out.print("\n->");
        for (;;) {
            String command = reader.readLine();
            if ("quit".equals(command)) {

                System.exit(0);

            } else if ("help".equals(command)) {

                System.out.println("\nCommands:\n");
                System.out.println("quit           ->  Exit the program");
                System.out.println("setboard [fen] ->  Set to board to the fen string");
                System.out.println("perft [depth]  ->  Run a perft check to [depth]");
                System.out.print("\n->");

            } else if (command.startsWith("setboard ")) {
                board.inputFen(command.substring(9)); // position               
                System.out.print("\n->");

            } else {
                System.out.println("Command not found.");
                System.out.print("\n->");
            }
            //PERFT
//            else if (command.startsWith("perft ")) {
//                
//                try {
//                    if (Integer.parseInt(command.substring(6)) <= 0) {
//                        System.out.println("Depth needs to be higher than 0.");
//                    } else {
//                        System.out.println(Perft.perft(board, Integer.parseInt(command.substring(6)), false));
//                    }
//                } catch (NumberFormatException ex) {
//                    System.out.println("Depth needs to be an integer.");
//                } catch (StringIndexOutOfBoundsException ex) {
//                    System.out.println("Please enter a search depth.");
//                }
//                System.out.print("\n->");
//
//            } 

        }
    }

    @Override
    public String commandToFEN(String position) throws ArrayIndexOutOfBoundsException {
        String[] splitString = position.split(" ");

        String fen = "";
        if (splitString.length < 6) {
            System.out.println("position fen error syntax error");
        } else {
            fen += splitString[2] + " "; // Pieces sur echiquier
            fen += splitString[3] + " "; // couleur qui a le trait
            fen += splitString[4] + " "; // droits de roquer
            fen += splitString[5] + " "; // En passant
            if (splitString.length >= 8) {
                fen += splitString[6] + " "; // demi-coups
                fen += splitString[7]; // coups
            }
        }

        return fen;
    }

    public IIA getIA() {
        return pIA;
    }

    public void setIA(IIA pIA) {
        this.pIA = pIA;
    }

    public static void main(String[] args) throws IOException {
        new IHM();
    }
}
//                System.out.println("divide [depth] ->  Run a divide check to [depth]");
//                System.out.println("searchd [depth]->  Search the position to [depth]");
//                System.out.println("searcht [time] ->  Search the position for [time] (in milliseconds)");
//                System.out.println("eval           ->  Evaluates the position");
//    public String isGameOver(IBoard board, int[] gameHistory, int gameHistoryIndex) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//    public void uci() throws IOException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    public void winBoard() throws IOException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }

